<template>
  <div
    class="site-wrapper"
    :class="{ 'site-sidebar--fold': sidebarFold }"
    v-loading.fullscreen.lock="loading"
    element-loading-text="拼命加载中">
    <template v-if="!loading">
      <main-navbar @refreshInfo="refreshInfo" ref="mainNavbar"/>
      <main-sidebar />
      <div class="site-content__wrapper" :style="(topMenu == 'tm01'?'margin-left: 0px;':'')+'min-height:'+ documentClientHeight + 'px'">
        <main-content />
      </div>
    </template>
  </div>
</template>

<script setup>
import { ref, computed, onMounted ,getCurrentInstance} from 'vue'
import { useUserStore } from '@/stores/user'
import { useCommonStore } from '@/stores/common'
import { storeToRefs } from 'pinia'
const { proxy } = getCurrentInstance() // 获取全局属性代理
const { $http } = proxy
const userStore = useUserStore()
const commonStore = useCommonStore()
const { 
  documentClientHeight: commonHeight,
  sidebarFold,
  topMenu
} = storeToRefs(commonStore)
const { 
  id: userId,
  name: userName,
  nameZw: name,
  bianshiid,
  bianshiname,
  roleName,
  roleId,
  publisherid,
  branchid,
  superAdmin,
  publisherStyle,
  isadmin,
  jsqx
} = storeToRefs(userStore)

const loading = ref(true)
const mainNavbar = ref(null)

// computed属性转换
const documentClientHeight = computed({
  get: () => commonHeight.value,
  set: (val) => commonStore.updateDocumentClientHeight(val)
})

// 生命周期
onMounted(() => {
  resetDocumentClientHeight()
  // 初始化调用
getUserInfo()
getCwrRceivedJeDisplay()
getPublisherid() 
sessionStorage.getItem('toggleType') && sessionStorage.removeItem('toggleType')
})

// 方法部分
const refreshInfo = () => {
  getUserInfo()
  getCwrRceivedJeDisplay()
  getPublisherid()
  resetDocumentClientHeight()
  mainNavbar.value.getMenuMain()
  mainNavbar.value.getFindRelation()
}

const getCwrRceivedJeDisplay = () => {
  $http({
    url: $http.adornUrl("/ty/ty/getRedisValueByKey"),
    method: "get",
    params: $http.adornParams({ key: "CW_RECEIVED_JE_DISPLAY" }),
  }).then(({ data }) => {
    if (data?.code === 0) {
      const paramValue = data.list?.paramvalue || ''
      commonStore.updateCwrRceivedJeDisplay(paramValue)
    }
  })
}

const resetDocumentClientHeight = () => {
  documentClientHeight.value = document.documentElement.clientHeight
  window.onresize = () => {
    documentClientHeight.value = document.documentElement.clientHeight
  }
}

const getUserInfo = () => {
  $http({
    url: $http.adornUrl('/sys/user/info'),
    method: 'get'
  }).then(({ data }) => {
    if (data?.code === 0) {
      loading.value = false
      // Pinia状态更新
      userStore.$patch({
        id: data.user.userId,
        name: data.user.username,
        nameZw: data.user.name,
        bianshiid: data.user.bianshiid,
        bianshiname: data.user.bianshiname,
        branchid: data.user.branchid,
        superAdmin: data.user.superAdmin,
        isadmin: data.user.isadmin,
        roleName: data.user.roleName || '',
        roleId: data.user.roleId,
        publisherid: data.user.publisherid,
        jsqx: data.user.jsqx,
        publisherStyle: data.user.publisherStyle
      })
      localStorage.setItem("cbsOrFx", "CBS")
      localStorage.setItem("fhdzfs", "HZ")
    }
  })
}

const getPublisherid = () => {
  $http({
    url: $http.adornUrl('/sys/getPublisherid'),
    method: 'get'
  }).then(({ data }) => {
    if (data?.code === 0 && data.userPublisherid === 'tjjt') {
      userStore.updatePublisherType(data.userPublisherid)
    }
  })
}


</script>
